home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2002 November / SGI IRIX Base Documentation 2002 November.iso / usr / share / catman / u_man / cat1 / perlxs.z / perlxs
Encoding:
Text File  |  2002-10-03  |  59.2 KB  |  1,651 lines

  1.  
  2.  
  3.  
  4. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      perlxs - XS language reference manual
  10.  
  11. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.      IIIInnnnttttrrrroooodddduuuuccccttttiiiioooonnnn
  13.  
  14.      XS is a language used to create an extension interface between Perl and
  15.      some C library which one wishes to use with Perl.  The XS interface is
  16.      combined with the library to create a new library which can be linked to
  17.      Perl.  An XXXXSSSSUUUUBBBB is a function in the XS language and is the core component
  18.      of the Perl application interface.
  19.  
  20.      The XS compiler is called xxxxssssuuuubbbbpppppppp.  This compiler will embed the
  21.      constructs necessary to let an XSUB, which is really a C function in
  22.      disguise, manipulate Perl values and creates the glue necessary to let
  23.      Perl access the XSUB.  The compiler uses ttttyyyyppppeeeemmmmaaaappppssss to determine how to map
  24.      C function parameters and variables to Perl values.  The default typemap
  25.      handles many common C types.  A supplement typemap must be created to
  26.      handle special structures and types for the library being linked.
  27.  
  28.      See the _p_e_r_l_x_s_t_u_t manpage for a tutorial on the whole extension creation
  29.      process.
  30.  
  31.      Note: For many extensions, Dave Beazley's SWIG system provides a
  32.      significantly more convenient mechanism for creating the XS glue code.
  33.      See the section on /_w_w_w._c_s._u_t_a_h._e_d_u/~_b_e_a_z_l_e_y/_S_W_I_G in the _h_t_t_p: manpage
  34.      for more information.
  35.  
  36.      OOOOnnnn TTTThhhheeee RRRRooooaaaadddd
  37.  
  38.      Many of the examples which follow will concentrate on creating an
  39.      interface between Perl and the ONC+ RPC bind library functions.  The
  40.      _r_p_c_b__g_e_t_t_i_m_e() function is used to demonstrate many features of the XS
  41.      language.  This function has two parameters; the first is an input
  42.      parameter and the second is an output parameter.  The function also
  43.      returns a status value.
  44.  
  45.              bool_t rpcb_gettime(const char *host, time_t *timep);
  46.  
  47.      From C this function will be called with the following statements.
  48.  
  49.           #include <rpc/rpc.h>
  50.           bool_t status;
  51.           time_t timep;
  52.           status = rpcb_gettime( "localhost", &timep );
  53.  
  54.      If an XSUB is created to offer a direct translation between this function
  55.      and Perl, then this XSUB will be used from Perl with the following code.
  56.      The $status and $timep variables will contain the output of the function.
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  71.  
  72.  
  73.  
  74.           use RPC;
  75.           $status = rpcb_gettime( "localhost", $timep );
  76.  
  77.      The following XS file shows an XS subroutine, or XSUB, which demonstrates
  78.      one possible interface to the _r_p_c_b__g_e_t_t_i_m_e() function.  This XSUB
  79.      represents a direct translation between C and Perl and so preserves the
  80.      interface even from Perl.  This XSUB will be invoked from Perl with the
  81.      usage shown above.  Note that the first three #include statements, for
  82.      EXTERN.h, perl.h, and XSUB.h, will always be present at the beginning of
  83.      an XS file.  This approach and others will be expanded later in this
  84.      document.
  85.  
  86.           #include "EXTERN.h"
  87.           #include "perl.h"
  88.           #include "XSUB.h"
  89.           #include <rpc/rpc.h>
  90.  
  91.           MODULE = RPC  PACKAGE = RPC
  92.  
  93.           bool_t
  94.           rpcb_gettime(host,timep)
  95.                char *host
  96.                time_t &timep
  97.                OUTPUT:
  98.                timep
  99.  
  100.      Any extension to Perl, including those containing XSUBs, should have a
  101.      Perl module to serve as the bootstrap which pulls the extension into
  102.      Perl.  This module will export the extension's functions and variables to
  103.      the Perl program and will cause the extension's XSUBs to be linked into
  104.      Perl.  The following module will be used for most of the examples in this
  105.      document and should be used from Perl with the use command as shown
  106.      earlier.  Perl modules are explained in more detail later in this
  107.      document.
  108.  
  109.           package RPC;
  110.  
  111.           require Exporter;
  112.           require DynaLoader;
  113.           @ISA = qw(Exporter DynaLoader);
  114.           @EXPORT = qw( rpcb_gettime );
  115.  
  116.           bootstrap RPC;
  117.           1;
  118.  
  119.      Throughout this document a variety of interfaces to the _r_p_c_b__g_e_t_t_i_m_e()
  120.      XSUB will be explored.  The XSUBs will take their parameters in different
  121.      orders or will take different numbers of parameters.  In each case the
  122.      XSUB is an abstraction between Perl and the real C _r_p_c_b__g_e_t_t_i_m_e()
  123.      function, and the XSUB must always ensure that the real _r_p_c_b__g_e_t_t_i_m_e()
  124.      function is called with the correct parameters.  This abstraction will
  125.      allow the programmer to create a more Perl-like interface to the C
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  137.  
  138.  
  139.  
  140.      function.
  141.  
  142.      TTTThhhheeee AAAAnnnnaaaattttoooommmmyyyy ooooffff aaaannnn XXXXSSSSUUUUBBBB
  143.  
  144.      The following XSUB allows a Perl program to access a C library function
  145.      called _s_i_n().  The XSUB will imitate the C function which takes a single
  146.      argument and returns a single value.
  147.  
  148.           double
  149.           sin(x)
  150.             double x
  151.  
  152.      When using C pointers the indirection operator * should be considered
  153.      part of the type and the address operator & should be considered part of
  154.      the variable, as is demonstrated in the _r_p_c_b__g_e_t_t_i_m_e() function above.
  155.      See the section on typemaps for more about handling qualifiers and unary
  156.      operators in C types.
  157.  
  158.      The function name and the return type must be placed on separate lines.
  159.  
  160.        INCORRECT                        CORRECT
  161.  
  162.        double sin(x)                    double
  163.          double x                       sin(x)
  164.                                           double x
  165.  
  166.      The function body may be indented or left-adjusted.  The following
  167.      example shows a function with its body left-adjusted.  Most examples in
  168.      this document will indent the body.
  169.  
  170.        CORRECT
  171.  
  172.        double
  173.        sin(x)
  174.        double x
  175.  
  176.  
  177.      TTTThhhheeee AAAArrrrgggguuuummmmeeeennnntttt SSSSttttaaaacccckkkk
  178.  
  179.      The argument stack is used to store the values which are sent as
  180.      parameters to the XSUB and to store the XSUB's return value.  In reality
  181.      all Perl functions keep their values on this stack at the same time, each
  182.      limited to its own range of positions on the stack.  In this document the
  183.      first position on that stack which belongs to the active function will be
  184.      referred to as position 0 for that function.
  185.  
  186.      XSUBs refer to their stack arguments with the macro SSSSTTTT((((xxxx)))), where _x refers
  187.      to a position in this XSUB's part of the stack.  Position 0 for that
  188.      function would be known to the XSUB as _S_T(0).  The XSUB's incoming
  189.      parameters and outgoing return values always begin at _S_T(0).  For many
  190.      simple cases the xxxxssssuuuubbbbpppppppp compiler will generate the code necessary to
  191.      handle the argument stack by embedding code fragments found in the
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  203.  
  204.  
  205.  
  206.      typemaps.  In more complex cases the programmer must supply the code.
  207.  
  208.      TTTThhhheeee RRRREEEETTTTVVVVAAAALLLL VVVVaaaarrrriiiiaaaabbbblllleeee
  209.  
  210.      The RETVAL variable is a magic variable which always matches the return
  211.      type of the C library function.  The xxxxssssuuuubbbbpppppppp compiler will supply this
  212.      variable in each XSUB and by default will use it to hold the return value
  213.      of the C library function being called.  In simple cases the value of
  214.      RETVAL will be placed in _S_T(0) of the argument stack where it can be
  215.      received by Perl as the return value of the XSUB.
  216.  
  217.      If the XSUB has a return type of void then the compiler will not supply a
  218.      RETVAL variable for that function.  When using the PPCODE: directive the
  219.      RETVAL variable is not needed, unless used explicitly.
  220.  
  221.      If PPCODE: directive is not used, void return value should be used only
  222.      for subroutines which do not return a value, _e_v_e_n _i_f CODE:  directive is
  223.      used which sets _S_T(0) explicitly.
  224.  
  225.      Older versions of this document recommended to use void return value in
  226.      such cases. It was discovered that this could lead to segfaults in cases
  227.      when XSUB was _t_r_u_e_l_y void. This practice is now deprecated, and may be
  228.      not supported at some future version. Use the return value SV * in such
  229.      cases. (Currently xsubpp contains some heuristic code which tries to
  230.      disambiguate between "truely-void" and "old-practice-declared-as-void"
  231.      functions. Hence your code is at mercy of this heuristics unless you use
  232.      SV * as return value.)
  233.  
  234.      TTTThhhheeee MMMMOOOODDDDUUUULLLLEEEE KKKKeeeeyyyywwwwoooorrrrdddd
  235.  
  236.      The MODULE keyword is used to start the XS code and to specify the
  237.      package of the functions which are being defined.  All text preceding the
  238.      first MODULE keyword is considered C code and is passed through to the
  239.      output untouched.  Every XS module will have a bootstrap function which
  240.      is used to hook the XSUBs into Perl.  The package name of this bootstrap
  241.      function will match the value of the last MODULE statement in the XS
  242.      source files.  The value of MODULE should always remain constant within
  243.      the same XS file, though this is not required.
  244.  
  245.      The following example will start the XS code and will place all functions
  246.      in a package named RPC.
  247.  
  248.           MODULE = RPC
  249.  
  250.  
  251.      TTTThhhheeee PPPPAAAACCCCKKKKAAAAGGGGEEEE KKKKeeeeyyyywwwwoooorrrrdddd
  252.  
  253.      When functions within an XS source file must be separated into packages
  254.      the PACKAGE keyword should be used.  This keyword is used with the MODULE
  255.      keyword and must follow immediately after it when used.
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  269.  
  270.  
  271.  
  272.           MODULE = RPC  PACKAGE = RPC
  273.  
  274.           [ XS code in package RPC ]
  275.  
  276.           MODULE = RPC  PACKAGE = RPCB
  277.  
  278.           [ XS code in package RPCB ]
  279.  
  280.           MODULE = RPC  PACKAGE = RPC
  281.  
  282.           [ XS code in package RPC ]
  283.  
  284.      Although this keyword is optional and in some cases provides redundant
  285.      information it should always be used.  This keyword will ensure that the
  286.      XSUBs appear in the desired package.
  287.  
  288.      TTTThhhheeee PPPPRRRREEEEFFFFIIIIXXXX KKKKeeeeyyyywwwwoooorrrrdddd
  289.  
  290.      The PREFIX keyword designates prefixes which should be removed from the
  291.      Perl function names.  If the C function is rpcb_gettime() and the PREFIX
  292.      value is rpcb_ then Perl will see this function as gettime().
  293.  
  294.      This keyword should follow the PACKAGE keyword when used.  If PACKAGE is
  295.      not used then PREFIX should follow the MODULE keyword.
  296.  
  297.           MODULE = RPC  PREFIX = rpc_
  298.  
  299.           MODULE = RPC  PACKAGE = RPCB  PREFIX = rpcb_
  300.  
  301.  
  302.      TTTThhhheeee OOOOUUUUTTTTPPPPUUUUTTTT:::: KKKKeeeeyyyywwwwoooorrrrdddd
  303.  
  304.      The OUTPUT: keyword indicates that certain function parameters should be
  305.      updated (new values made visible to Perl) when the XSUB terminates or
  306.      that certain values should be returned to the calling Perl function.  For
  307.      simple functions, such as the _s_i_n() function above, the RETVAL variable
  308.      is automatically designated as an output value.  In more complex
  309.      functions the xxxxssssuuuubbbbpppppppp compiler will need help to determine which variables
  310.      are output variables.
  311.  
  312.      This keyword will normally be used to complement the CODE:  keyword.  The
  313.      RETVAL variable is not recognized as an output variable when the CODE:
  314.      keyword is present.  The OUTPUT:  keyword is used in this situation to
  315.      tell the compiler that RETVAL really is an output variable.
  316.  
  317.      The OUTPUT: keyword can also be used to indicate that function parameters
  318.      are output variables.  This may be necessary when a parameter has been
  319.      modified within the function and the programmer would like the update to
  320.      be seen by Perl.
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  335.  
  336.  
  337.  
  338.           bool_t
  339.           rpcb_gettime(host,timep)
  340.                char *host
  341.                time_t &timep
  342.                OUTPUT:
  343.                timep
  344.  
  345.      The OUTPUT: keyword will also allow an output parameter to be mapped to a
  346.      matching piece of code rather than to a typemap.
  347.  
  348.           bool_t
  349.           rpcb_gettime(host,timep)
  350.                char *host
  351.                time_t &timep
  352.                OUTPUT:
  353.                timep sv_setnv(ST(1), (double)timep);
  354.  
  355.      xxxxssssuuuubbbbpppppppp emits an automatic SvSETMAGIC() for all parameters in the OUTPUT
  356.      section of the XSUB, except RETVAL.  This is the usually desired
  357.      behavior, as it takes care of properly invoking 'set' magic on output
  358.      parameters (needed for hash or array element parameters that must be
  359.      created if they didn't exist).  If for some reason, this behavior is not
  360.      desired, the OUTPUT section may contain a SETMAGIC: DISABLE line to
  361.      disable it for the remainder of the parameters in the OUTPUT section.
  362.      Likewise,  SETMAGIC: ENABLE can be used to reenable it for the remainder
  363.      of the OUTPUT section.  See the _p_e_r_l_g_u_t_s manpage for more details about
  364.      'set' magic.
  365.  
  366.      TTTThhhheeee CCCCOOOODDDDEEEE:::: KKKKeeeeyyyywwwwoooorrrrdddd
  367.  
  368.      This keyword is used in more complicated XSUBs which require special
  369.      handling for the C function.  The RETVAL variable is available but will
  370.      not be returned unless it is specified under the OUTPUT: keyword.
  371.  
  372.      The following XSUB is for a C function which requires special handling of
  373.      its parameters.  The Perl usage is given first.
  374.  
  375.           $status = rpcb_gettime( "localhost", $timep );
  376.  
  377.      The XSUB follows.
  378.  
  379.           bool_t
  380.           rpcb_gettime(host,timep)
  381.                char *host
  382.                time_t timep
  383.                CODE:
  384.                     RETVAL = rpcb_gettime( host, &timep );
  385.                OUTPUT:
  386.                timep
  387.                RETVAL
  388.  
  389.  
  390.  
  391.  
  392.  
  393.                                                                         PPPPaaaaggggeeee 6666
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  401.  
  402.  
  403.  
  404.      TTTThhhheeee IIIINNNNIIIITTTT:::: KKKKeeeeyyyywwwwoooorrrrdddd
  405.  
  406.      The INIT: keyword allows initialization to be inserted into the XSUB
  407.      before the compiler generates the call to the C function.  Unlike the
  408.      CODE: keyword above, this keyword does not affect the way the compiler
  409.      handles RETVAL.
  410.  
  411.          bool_t
  412.          rpcb_gettime(host,timep)
  413.                char *host
  414.                time_t &timep
  415.                INIT:
  416.                printf("# Host is %s\n", host );
  417.                OUTPUT:
  418.                timep
  419.  
  420.  
  421.      TTTThhhheeee NNNNOOOO____IIIINNNNIIIITTTT KKKKeeeeyyyywwwwoooorrrrdddd
  422.  
  423.      The NO_INIT keyword is used to indicate that a function parameter is
  424.      being used only as an output value.  The xxxxssssuuuubbbbpppppppp compiler will normally
  425.      generate code to read the values of all function parameters from the
  426.      argument stack and assign them to C variables upon entry to the function.
  427.      NO_INIT will tell the compiler that some parameters will be used for
  428.      output rather than for input and that they will be handled before the
  429.      function terminates.
  430.  
  431.      The following example shows a variation of the _r_p_c_b__g_e_t_t_i_m_e() function.
  432.      This function uses the timep variable only as an output variable and does
  433.      not care about its initial contents.
  434.  
  435.           bool_t
  436.           rpcb_gettime(host,timep)
  437.                char *host
  438.                time_t &timep = NO_INIT
  439.                OUTPUT:
  440.                timep
  441.  
  442.  
  443.      IIIInnnniiiittttiiiiaaaalllliiiizzzziiiinnnngggg FFFFuuuunnnnccccttttiiiioooonnnn PPPPaaaarrrraaaammmmeeeetttteeeerrrrssss
  444.  
  445.      Function parameters are normally initialized with their values from the
  446.      argument stack.  The typemaps contain the code segments which are used to
  447.      transfer the Perl values to the C parameters.  The programmer, however,
  448.      is allowed to override the typemaps and supply alternate (or additional)
  449.      initialization code.
  450.  
  451.      The following code demonstrates how to supply initialization code for
  452.      function parameters.  The initialization code is eval'd within double
  453.      quotes by the compiler before it is added to the output so anything which
  454.      should be interpreted literally [mainly $, @, or \\] must be protected
  455.      with backslashes.  The variables $var, $arg, and $type can be used as in
  456.  
  457.  
  458.  
  459.                                                                         PPPPaaaaggggeeee 7777
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  467.  
  468.  
  469.  
  470.      typemaps.
  471.  
  472.           bool_t
  473.           rpcb_gettime(host,timep)
  474.                char *host = (char *)SvPV($arg,PL_na);
  475.                time_t &timep = 0;
  476.                OUTPUT:
  477.                timep
  478.  
  479.      This should not be used to supply default values for parameters.  One
  480.      would normally use this when a function parameter must be processed by
  481.      another library function before it can be used.  Default parameters are
  482.      covered in the next section.
  483.  
  484.      DDDDeeeeffffaaaauuuulllltttt PPPPaaaarrrraaaammmmeeeetttteeeerrrr VVVVaaaalllluuuueeeessss
  485.  
  486.      Default values can be specified for function parameters by placing an
  487.      assignment statement in the parameter list.  The default value may be a
  488.      number or a string.  Defaults should always be used on the right-most
  489.      parameters only.
  490.  
  491.      To allow the XSUB for _r_p_c_b__g_e_t_t_i_m_e() to have a default host value the
  492.      parameters to the XSUB could be rearranged.  The XSUB will then call the
  493.      real _r_p_c_b__g_e_t_t_i_m_e() function with the parameters in the correct order.
  494.      Perl will call this XSUB with either of the following statements.
  495.  
  496.           $status = rpcb_gettime( $timep, $host );
  497.  
  498.           $status = rpcb_gettime( $timep );
  499.  
  500.      The XSUB will look like the code  which  follows.   A  CODE:  block  is
  501.      used to call the real _r_p_c_b__g_e_t_t_i_m_e() function with the parameters in the
  502.      correct order for that function.
  503.  
  504.           bool_t
  505.           rpcb_gettime(timep,host="localhost")
  506.                char *host
  507.                time_t timep = NO_INIT
  508.                CODE:
  509.                     RETVAL = rpcb_gettime( host, &timep );
  510.                OUTPUT:
  511.                timep
  512.                RETVAL
  513.  
  514.  
  515.      TTTThhhheeee PPPPRRRREEEEIIIINNNNIIIITTTT:::: KKKKeeeeyyyywwwwoooorrrrdddd
  516.  
  517.      The PREINIT: keyword allows extra variables to be declared before the
  518.      typemaps are expanded.  If a variable is declared in a CODE: block then
  519.      that variable will follow any typemap code.  This may result in a C
  520.      syntax error.  To force the variable to be declared before the typemap
  521.      code, place it into a PREINIT: block.  The PREINIT: keyword may be used
  522.  
  523.  
  524.  
  525.                                                                         PPPPaaaaggggeeee 8888
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  533.  
  534.  
  535.  
  536.      one or more times within an XSUB.
  537.  
  538.      The following examples are equivalent, but if the code is using complex
  539.      typemaps then the first example is safer.
  540.  
  541.           bool_t
  542.           rpcb_gettime(timep)
  543.                time_t timep = NO_INIT
  544.                PREINIT:
  545.                char *host = "localhost";
  546.                CODE:
  547.                RETVAL = rpcb_gettime( host, &timep );
  548.                OUTPUT:
  549.                timep
  550.                RETVAL
  551.  
  552.      A correct, but error-prone example.
  553.  
  554.           bool_t
  555.           rpcb_gettime(timep)
  556.                time_t timep = NO_INIT
  557.                CODE:
  558.                char *host = "localhost";
  559.                RETVAL = rpcb_gettime( host, &timep );
  560.                OUTPUT:
  561.                timep
  562.                RETVAL
  563.  
  564.  
  565.      TTTThhhheeee SSSSCCCCOOOOPPPPEEEE:::: KKKKeeeeyyyywwwwoooorrrrdddd
  566.  
  567.      The SCOPE: keyword allows scoping to be enabled for a particular XSUB. If
  568.      enabled, the XSUB will invoke ENTER and LEAVE automatically.
  569.  
  570.      To support potentially complex type mappings, if a typemap entry used by
  571.      this XSUB contains a comment like /*scope*/ then scoping will
  572.      automatically be enabled for that XSUB.
  573.  
  574.      To enable scoping:
  575.  
  576.          SCOPE: ENABLE
  577.  
  578.      To disable scoping:
  579.  
  580.          SCOPE: DISABLE
  581.  
  582.  
  583.      TTTThhhheeee IIIINNNNPPPPUUUUTTTT:::: KKKKeeeeyyyywwwwoooorrrrdddd
  584.  
  585.      The XSUB's parameters are usually evaluated immediately after entering
  586.      the XSUB.  The INPUT: keyword can be used to force those parameters to be
  587.      evaluated a little later.  The INPUT: keyword can be used multiple times
  588.  
  589.  
  590.  
  591.                                                                         PPPPaaaaggggeeee 9999
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  599.  
  600.  
  601.  
  602.      within an XSUB and can be used to list one or more input variables.  This
  603.      keyword is used with the PREINIT: keyword.
  604.  
  605.      The following example shows how the input parameter timep can be
  606.      evaluated late, after a PREINIT.
  607.  
  608.          bool_t
  609.          rpcb_gettime(host,timep)
  610.                char *host
  611.                PREINIT:
  612.                time_t tt;
  613.                INPUT:
  614.                time_t timep
  615.                CODE:
  616.                     RETVAL = rpcb_gettime( host, &tt );
  617.                     timep = tt;
  618.                OUTPUT:
  619.                timep
  620.                RETVAL
  621.  
  622.      The next example shows each input parameter evaluated late.
  623.  
  624.          bool_t
  625.          rpcb_gettime(host,timep)
  626.                PREINIT:
  627.                time_t tt;
  628.                INPUT:
  629.                char *host
  630.                PREINIT:
  631.                char *h;
  632.                INPUT:
  633.                time_t timep
  634.                CODE:
  635.                     h = host;
  636.                     RETVAL = rpcb_gettime( h, &tt );
  637.                     timep = tt;
  638.                OUTPUT:
  639.                timep
  640.                RETVAL
  641.  
  642.  
  643.      VVVVaaaarrrriiiiaaaabbbblllleeee----lllleeeennnnggggtttthhhh PPPPaaaarrrraaaammmmeeeetttteeeerrrr LLLLiiiissssttttssss
  644.  
  645.      XSUBs can have variable-length parameter lists by specifying an ellipsis
  646.      (...) in the parameter list.  This use of the ellipsis is similar to that
  647.      found in ANSI C.  The programmer is able to determine the number of
  648.      arguments passed to the XSUB by examining the items variable which the
  649.      xxxxssssuuuubbbbpppppppp compiler supplies for all XSUBs.  By using this mechanism one can
  650.      create an XSUB which accepts a list of parameters of unknown length.
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.                                                                        PPPPaaaaggggeeee 11110000
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  665.  
  666.  
  667.  
  668.      The _h_o_s_t parameter for the _r_p_c_b__g_e_t_t_i_m_e() XSUB can be optional so the
  669.      ellipsis can be used to indicate that the XSUB will take a variable
  670.      number of parameters.  Perl should be able to call this XSUB with either
  671.      of the following statements.
  672.  
  673.           $status = rpcb_gettime( $timep, $host );
  674.  
  675.           $status = rpcb_gettime( $timep );
  676.  
  677.      The XS code, with ellipsis, follows.
  678.  
  679.           bool_t
  680.           rpcb_gettime(timep, ...)
  681.                time_t timep = NO_INIT
  682.                PREINIT:
  683.                char *host = "localhost";
  684.                CODE:
  685.                        if( items > 1 )
  686.                             host = (char *)SvPV(ST(1), na);
  687.                        RETVAL = rpcb_gettime( host, &timep );
  688.                OUTPUT:
  689.                timep
  690.                RETVAL
  691.  
  692.  
  693.      TTTThhhheeee PPPPPPPPCCCCOOOODDDDEEEE:::: KKKKeeeeyyyywwwwoooorrrrdddd
  694.  
  695.      The PPCODE: keyword is an alternate form of the CODE: keyword and is used
  696.      to tell the xxxxssssuuuubbbbpppppppp compiler that the programmer is supplying the code to
  697.      control the argument stack for the XSUBs return values.  Occasionally one
  698.      will want an XSUB to return a list of values rather than a single value.
  699.      In these cases one must use PPCODE: and then explicitly push the list of
  700.      values on the stack.  The PPCODE: and CODE:  keywords are not used
  701.      together within the same XSUB.
  702.  
  703.      The following XSUB will call the C _r_p_c_b__g_e_t_t_i_m_e() function and will
  704.      return its two output values, timep and status, to Perl as a single list.
  705.  
  706.           void
  707.           rpcb_gettime(host)
  708.                char *host
  709.                PREINIT:
  710.                time_t  timep;
  711.                bool_t  status;
  712.                PPCODE:
  713.                status = rpcb_gettime( host, &timep );
  714.                EXTEND(SP, 2);
  715.                PUSHs(sv_2mortal(newSViv(status)));
  716.                PUSHs(sv_2mortal(newSViv(timep)));
  717.  
  718.      Notice that the programmer must supply the C code necessary to have the
  719.      real _r_p_c_b__g_e_t_t_i_m_e() function called and to have the return values
  720.  
  721.  
  722.  
  723.                                                                        PPPPaaaaggggeeee 11111111
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  731.  
  732.  
  733.  
  734.      properly placed on the argument stack.
  735.  
  736.      The void return type for this function tells the xxxxssssuuuubbbbpppppppp compiler that the
  737.      RETVAL variable is not needed or used and that it should not be created.
  738.      In most scenarios the void return type should be used with the PPCODE:
  739.      directive.
  740.  
  741.      The _E_X_T_E_N_D() macro is used to make room on the argument stack for 2
  742.      return values.  The PPCODE: directive causes the xxxxssssuuuubbbbpppppppp compiler to
  743.      create a stack pointer available as SP, and it is this pointer which is
  744.      being used in the _E_X_T_E_N_D() macro.  The values are then pushed onto the
  745.      stack with the _P_U_S_H_s() macro.
  746.  
  747.      Now the _r_p_c_b__g_e_t_t_i_m_e() function can be used from Perl with the following
  748.      statement.
  749.  
  750.           ($status, $timep) = rpcb_gettime("localhost");
  751.  
  752.      When handling output parameters with a PPCODE section, be sure to handle
  753.      'set' magic properly.  See the _p_e_r_l_g_u_t_s manpage for details about 'set'
  754.      magic.
  755.  
  756.      RRRReeeettttuuuurrrrnnnniiiinnnngggg UUUUnnnnddddeeeeffff AAAAnnnndddd EEEEmmmmppppttttyyyy LLLLiiiissssttttssss
  757.  
  758.      Occasionally the programmer will want to return simply undef or an empty
  759.      list if a function fails rather than a separate status value.  The
  760.      _r_p_c_b__g_e_t_t_i_m_e() function offers just this situation.  If the function
  761.      succeeds we would like to have it return the time and if it fails we
  762.      would like to have undef returned.  In the following Perl code the value
  763.      of $timep will either be undef or it will be a valid time.
  764.  
  765.           $timep = rpcb_gettime( "localhost" );
  766.  
  767.      The following XSUB uses the SV * return type as a mnemonic only, and uses
  768.      a CODE: block to indicate to the compiler that the programmer has
  769.      supplied all the necessary code.  The _s_v__n_e_w_m_o_r_t_a_l() call will initialize
  770.      the return value to undef, making that the default return value.
  771.  
  772.           SV *
  773.           rpcb_gettime(host)
  774.                char *  host
  775.                PREINIT:
  776.                time_t  timep;
  777.                bool_t x;
  778.                CODE:
  779.                ST(0) = sv_newmortal();
  780.                if( rpcb_gettime( host, &timep ) )
  781.                     sv_setnv( ST(0), (double)timep);
  782.  
  783.      The next example demonstrates how one would place an explicit undef in
  784.      the return value, should the need arise.
  785.  
  786.  
  787.  
  788.  
  789.                                                                        PPPPaaaaggggeeee 11112222
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  797.  
  798.  
  799.  
  800.           SV *
  801.           rpcb_gettime(host)
  802.                char *  host
  803.                PREINIT:
  804.                time_t  timep;
  805.                bool_t x;
  806.                CODE:
  807.                ST(0) = sv_newmortal();
  808.                if( rpcb_gettime( host, &timep ) ){
  809.                     sv_setnv( ST(0), (double)timep);
  810.                }
  811.                else{
  812.                     ST(0) = &sv_undef;
  813.                }
  814.  
  815.      To return an empty list one must use a PPCODE: block and then not push
  816.      return values on the stack.
  817.  
  818.           void
  819.           rpcb_gettime(host)
  820.                char *host
  821.                PREINIT:
  822.                time_t  timep;
  823.                PPCODE:
  824.                if( rpcb_gettime( host, &timep ) )
  825.                     PUSHs(sv_2mortal(newSViv(timep)));
  826.                else{
  827.                /* Nothing pushed on stack, so an empty */
  828.                /* list is implicitly returned. */
  829.                }
  830.  
  831.      Some people may be inclined to include an explicit return in the above
  832.      XSUB, rather than letting control fall through to the end.  In those
  833.      situations XSRETURN_EMPTY should be used, instead.  This will ensure that
  834.      the XSUB stack is properly adjusted.  Consult the section on _A_P_I _L_I_S_T_I_N_G
  835.      in the _p_e_r_l_g_u_t_s manpage for other XSRETURN macros.
  836.  
  837.      TTTThhhheeee RRRREEEEQQQQUUUUIIIIRRRREEEE:::: KKKKeeeeyyyywwwwoooorrrrdddd
  838.  
  839.      The REQUIRE: keyword is used to indicate the minimum version of the
  840.      xxxxssssuuuubbbbpppppppp compiler needed to compile the XS module.  An XS module which
  841.      contains the following statement will compile with only xxxxssssuuuubbbbpppppppp version
  842.      1.922 or greater:
  843.  
  844.              REQUIRE: 1.922
  845.  
  846.  
  847.      TTTThhhheeee CCCCLLLLEEEEAAAANNNNUUUUPPPP:::: KKKKeeeeyyyywwwwoooorrrrdddd
  848.  
  849.      This keyword can be used when an XSUB requires special cleanup procedures
  850.      before it terminates.  When the CLEANUP:  keyword is used it must follow
  851.      any CODE:, PPCODE:, or OUTPUT: blocks which are present in the XSUB.  The
  852.  
  853.  
  854.  
  855.                                                                        PPPPaaaaggggeeee 11113333
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  863.  
  864.  
  865.  
  866.      code specified for the cleanup block will be added as the last statements
  867.      in the XSUB.
  868.  
  869.      TTTThhhheeee BBBBOOOOOOOOTTTT:::: KKKKeeeeyyyywwwwoooorrrrdddd
  870.  
  871.      The BOOT: keyword is used to add code to the extension's bootstrap
  872.      function.  The bootstrap function is generated by the xxxxssssuuuubbbbpppppppp compiler and
  873.      normally holds the statements necessary to register any XSUBs with Perl.
  874.      With the BOOT: keyword the programmer can tell the compiler to add extra
  875.      statements to the bootstrap function.
  876.  
  877.      This keyword may be used any time after the first MODULE keyword and
  878.      should appear on a line by itself.  The first blank line after the
  879.      keyword will terminate the code block.
  880.  
  881.           BOOT:
  882.           # The following message will be printed when the
  883.           # bootstrap function executes.
  884.           printf("Hello from the bootstrap!\n");
  885.  
  886.  
  887.      TTTThhhheeee VVVVEEEERRRRSSSSIIIIOOOONNNNCCCCHHHHEEEECCCCKKKK:::: KKKKeeeeyyyywwwwoooorrrrdddd
  888.  
  889.      The VERSIONCHECK: keyword corresponds to xxxxssssuuuubbbbpppppppp's -versioncheck and
  890.      -noversioncheck options.  This keyword overrides the command line
  891.      options.  Version checking is enabled by default.  When version checking
  892.      is enabled the XS module will attempt to verify that its version matches
  893.      the version of the PM module.
  894.  
  895.      To enable version checking:
  896.  
  897.          VERSIONCHECK: ENABLE
  898.  
  899.      To disable version checking:
  900.  
  901.          VERSIONCHECK: DISABLE
  902.  
  903.  
  904.      TTTThhhheeee PPPPRRRROOOOTTTTOOOOTTTTYYYYPPPPEEEESSSS:::: KKKKeeeeyyyywwwwoooorrrrdddd
  905.  
  906.      The PROTOTYPES: keyword corresponds to xxxxssssuuuubbbbpppppppp's -prototypes and
  907.      -noprototypes options.  This keyword overrides the command line options.
  908.      Prototypes are enabled by default.  When prototypes are enabled XSUBs
  909.      will be given Perl prototypes.  This keyword may be used multiple times
  910.      in an XS module to enable and disable prototypes for different parts of
  911.      the module.
  912.  
  913.      To enable prototypes:
  914.  
  915.          PROTOTYPES: ENABLE
  916.  
  917.      To disable prototypes:
  918.  
  919.  
  920.  
  921.                                                                        PPPPaaaaggggeeee 11114444
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  929.  
  930.  
  931.  
  932.          PROTOTYPES: DISABLE
  933.  
  934.  
  935.      TTTThhhheeee PPPPRRRROOOOTTTTOOOOTTTTYYYYPPPPEEEE:::: KKKKeeeeyyyywwwwoooorrrrdddd
  936.  
  937.      This keyword is similar to the PROTOTYPES: keyword above but can be used
  938.      to force xxxxssssuuuubbbbpppppppp to use a specific prototype for the XSUB.  This keyword
  939.      overrides all other prototype options and keywords but affects only the
  940.      current XSUB.  Consult the Prototypes entry in the _p_e_r_l_s_u_b manpage for
  941.      information about Perl prototypes.
  942.  
  943.          bool_t
  944.          rpcb_gettime(timep, ...)
  945.                time_t timep = NO_INIT
  946.                PROTOTYPE: $;$
  947.                PREINIT:
  948.                char *host = "localhost";
  949.                CODE:
  950.                        if( items > 1 )
  951.                             host = (char *)SvPV(ST(1), na);
  952.                        RETVAL = rpcb_gettime( host, &timep );
  953.                OUTPUT:
  954.                timep
  955.                RETVAL
  956.  
  957.  
  958.      TTTThhhheeee AAAALLLLIIIIAAAASSSS:::: KKKKeeeeyyyywwwwoooorrrrdddd
  959.  
  960.      The ALIAS: keyword allows an XSUB to have two or more unique Perl names
  961.      and to know which of those names was used when it was invoked.  The Perl
  962.      names may be fully-qualified with package names.  Each alias is given an
  963.      index.  The compiler will setup a variable called ix which contain the
  964.      index of the alias which was used.  When the XSUB is called with its
  965.      declared name ix will be 0.
  966.  
  967.      The following example will create aliases FOO::gettime() and BAR::getit()
  968.      for this function.
  969.  
  970.          bool_t
  971.          rpcb_gettime(host,timep)
  972.                char *host
  973.                time_t &timep
  974.                ALIAS:
  975.                  FOO::gettime = 1
  976.                  BAR::getit = 2
  977.                INIT:
  978.                printf("# ix = %d\n", ix );
  979.                OUTPUT:
  980.                timep
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.                                                                        PPPPaaaaggggeeee 11115555
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  995.  
  996.  
  997.  
  998.      TTTThhhheeee IIIINNNNCCCCLLLLUUUUDDDDEEEE:::: KKKKeeeeyyyywwwwoooorrrrdddd
  999.  
  1000.      This keyword can be used to pull other files into the XS module.  The
  1001.      other files may have XS code.  INCLUDE: can also be used to run a command
  1002.      to generate the XS code to be pulled into the module.
  1003.  
  1004.      The file _R_p_c_b_1._x_s_h contains our rpcb_gettime() function:
  1005.  
  1006.          bool_t
  1007.          rpcb_gettime(host,timep)
  1008.                char *host
  1009.                time_t &timep
  1010.                OUTPUT:
  1011.                timep
  1012.  
  1013.      The XS module can use INCLUDE: to pull that file into it.
  1014.  
  1015.          INCLUDE: Rpcb1.xsh
  1016.  
  1017.      If the parameters to the INCLUDE: keyword are followed by a pipe (|) then
  1018.      the compiler will interpret the parameters as a command.
  1019.  
  1020.          INCLUDE: cat Rpcb1.xsh |
  1021.  
  1022.  
  1023.      TTTThhhheeee CCCCAAAASSSSEEEE:::: KKKKeeeeyyyywwwwoooorrrrdddd
  1024.  
  1025.      The CASE: keyword allows an XSUB to have multiple distinct parts with
  1026.      each part acting as a virtual XSUB.  CASE: is greedy and if it is used
  1027.      then all other XS keywords must be contained within a CASE:.  This means
  1028.      nothing may precede the first CASE: in the XSUB and anything following
  1029.      the last CASE: is included in that case.
  1030.  
  1031.      A CASE: might switch via a parameter of the XSUB, via the ix ALIAS:
  1032.      variable (see the section on _T_h_e _A_L_I_A_S: _K_e_y_w_o_r_d), or maybe via the items
  1033.      variable (see the section on _V_a_r_i_a_b_l_e-_l_e_n_g_t_h _P_a_r_a_m_e_t_e_r _L_i_s_t_s).  The last
  1034.      CASE: becomes the ddddeeeeffffaaaauuuulllltttt case if it is not associated with a
  1035.      conditional.  The following example shows CASE switched via ix with a
  1036.      function rpcb_gettime() having an alias x_gettime().  When the function
  1037.      is called as rpcb_gettime() its parameters are the usual (char *host,
  1038.      time_t *timep), but when the function is called as x_gettime() its
  1039.      parameters are reversed, (time_t *timep, char *host).
  1040.  
  1041.  
  1042.  
  1043.  
  1044.  
  1045.  
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.                                                                        PPPPaaaaggggeeee 11116666
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1061.  
  1062.  
  1063.  
  1064.          long
  1065.          rpcb_gettime(a,b)
  1066.            CASE: ix == 1
  1067.                ALIAS:
  1068.                x_gettime = 1
  1069.                INPUT:
  1070.                # 'a' is timep, 'b' is host
  1071.                char *b
  1072.                time_t a = NO_INIT
  1073.                CODE:
  1074.                     RETVAL = rpcb_gettime( b, &a );
  1075.                OUTPUT:
  1076.                a
  1077.                RETVAL
  1078.            CASE:
  1079.                # 'a' is host, 'b' is timep
  1080.                char *a
  1081.                time_t &b = NO_INIT
  1082.                OUTPUT:
  1083.                b
  1084.                RETVAL
  1085.  
  1086.      That function can be called with either of the following statements.
  1087.      Note the different argument lists.
  1088.  
  1089.              $status = rpcb_gettime( $host, $timep );
  1090.  
  1091.              $status = x_gettime( $timep, $host );
  1092.  
  1093.  
  1094.      TTTThhhheeee &&&& UUUUnnnnaaaarrrryyyy OOOOppppeeeerrrraaaattttoooorrrr
  1095.  
  1096.      The & unary operator is used to tell the compiler that it should
  1097.      dereference the object when it calls the C function.  This is used when a
  1098.      CODE: block is not used and the object is a not a pointer type (the
  1099.      object is an int or long but not a int* or long*).
  1100.  
  1101.      The following XSUB will generate incorrect C code.  The xsubpp compiler
  1102.      will turn this into code which calls rpcb_gettime() with parameters (char
  1103.      *host, time_t timep), but the real rpcb_gettime() wants the timep
  1104.      parameter to be of type time_t* rather than time_t.
  1105.  
  1106.          bool_t
  1107.          rpcb_gettime(host,timep)
  1108.                char *host
  1109.                time_t timep
  1110.                OUTPUT:
  1111.                timep
  1112.  
  1113.      That problem is corrected by using the & operator.  The xsubpp compiler
  1114.      will now turn this into code which calls rpcb_gettime() correctly with
  1115.      parameters (char *host, time_t *timep).  It does this by carrying the &
  1116.  
  1117.  
  1118.  
  1119.                                                                        PPPPaaaaggggeeee 11117777
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1127.  
  1128.  
  1129.  
  1130.      through, so the function call looks like rpcb_gettime(host, &timep).
  1131.  
  1132.          bool_t
  1133.          rpcb_gettime(host,timep)
  1134.                char *host
  1135.                time_t &timep
  1136.                OUTPUT:
  1137.                timep
  1138.  
  1139.  
  1140.      IIIInnnnsssseeeerrrrttttiiiinnnngggg CCCCoooommmmmmmmeeeennnnttttssss aaaannnndddd CCCC PPPPrrrreeeepppprrrroooocccceeeessssssssoooorrrr DDDDiiiirrrreeeeccccttttiiiivvvveeeessss
  1141.  
  1142.      C preprocessor directives are allowed within BOOT:, PREINIT: INIT:,
  1143.      CODE:, PPCODE:, and CLEANUP: blocks, as well as outside the functions.
  1144.      Comments are allowed anywhere after the MODULE keyword.  The compiler
  1145.      will pass the preprocessor directives through untouched and will remove
  1146.      the commented lines.
  1147.  
  1148.      Comments can be added to XSUBs by placing a # as the first non-whitespace
  1149.      of a line.  Care should be taken to avoid making the comment look like a
  1150.      C preprocessor directive, lest it be interpreted as such.  The simplest
  1151.      way to prevent this is to put whitespace in front of the #.
  1152.  
  1153.      If you use preprocessor directives to choose one of two versions of a
  1154.      function, use
  1155.  
  1156.          #if ... version1
  1157.          #else /* ... version2  */
  1158.          #endif
  1159.  
  1160.      and not
  1161.  
  1162.          #if ... version1
  1163.          #endif
  1164.          #if ... version2
  1165.          #endif
  1166.  
  1167.      because otherwise xsubpp will believe that you made a duplicate
  1168.      definition of the function.  Also, put a blank line before the
  1169.      #else/#endif so it will not be seen as part of the function body.
  1170.  
  1171.      UUUUssssiiiinnnngggg XXXXSSSS WWWWiiiitttthhhh CCCC++++++++
  1172.  
  1173.      If a function is defined as a C++ method then it will assume its first
  1174.      argument is an object pointer.  The object pointer will be stored in a
  1175.      variable called THIS.  The object should have been created by C++ with
  1176.      the _n_e_w() function and should be blessed by Perl with the _s_v__s_e_t_r_e_f__p_v()
  1177.      macro.  The blessing of the object by Perl can be handled by a typemap.
  1178.      An example typemap is shown at the end of this section.
  1179.  
  1180.  
  1181.  
  1182.  
  1183.  
  1184.  
  1185.                                                                        PPPPaaaaggggeeee 11118888
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1193.  
  1194.  
  1195.  
  1196.      If the method is defined as static it will call the C++ function using
  1197.      the _c_l_a_s_s::_m_e_t_h_o_d() syntax.  If the method is not static the function
  1198.      will be called using the THIS->_m_e_t_h_o_d() syntax.
  1199.  
  1200.      The next examples will use the following C++ class.
  1201.  
  1202.           class color {
  1203.                public:
  1204.                color();
  1205.                ~color();
  1206.                int blue();
  1207.                void set_blue( int );
  1208.  
  1209.                private:
  1210.                int c_blue;
  1211.           };
  1212.  
  1213.      The XSUBs for the _b_l_u_e() and _s_e_t__b_l_u_e() methods are defined with the
  1214.      class name but the parameter for the object (THIS, or "self") is implicit
  1215.      and is not listed.
  1216.  
  1217.           int
  1218.           color::blue()
  1219.  
  1220.           void
  1221.           color::set_blue( val )
  1222.                int val
  1223.  
  1224.      Both functions will expect an object as the first parameter.  The xsubpp
  1225.      compiler will call that object THIS and will use it to call the specified
  1226.      method.  So in the C++ code the _b_l_u_e() and _s_e_t__b_l_u_e() methods will be
  1227.      called in the following manner.
  1228.  
  1229.           RETVAL = THIS->blue();
  1230.  
  1231.           THIS->set_blue( val );
  1232.  
  1233.      If the function's name is DDDDEEEESSSSTTTTRRRROOOOYYYY then the C++ delete function will be
  1234.      called and THIS will be given as its parameter.
  1235.  
  1236.           void
  1237.           color::DESTROY()
  1238.  
  1239.      The C++ code will call delete.
  1240.  
  1241.           delete THIS;
  1242.  
  1243.      If the function's name is nnnneeeewwww then the C++ new function will be called to
  1244.      create a dynamic C++ object.  The XSUB will expect the class name, which
  1245.      will be kept in a variable called CLASS, to be given as the first
  1246.      argument.
  1247.  
  1248.  
  1249.  
  1250.  
  1251.                                                                        PPPPaaaaggggeeee 11119999
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1259.  
  1260.  
  1261.  
  1262.           color *
  1263.           color::new()
  1264.  
  1265.      The C++ code will call new.
  1266.  
  1267.              RETVAL = new color();
  1268.  
  1269.      The following is an example of a typemap that could be used for this C++
  1270.      example.
  1271.  
  1272.          TYPEMAP
  1273.          color *             O_OBJECT
  1274.  
  1275.          OUTPUT
  1276.          # The Perl object is blessed into 'CLASS', which should be a
  1277.          # char* having the name of the package for the blessing.
  1278.          O_OBJECT
  1279.              sv_setref_pv( $arg, CLASS, (void*)$var );
  1280.  
  1281.          INPUT
  1282.          O_OBJECT
  1283.              if( sv_isobject($arg) && (SvTYPE(SvRV($arg)) == SVt_PVMG) )
  1284.                      $var = ($type)SvIV((SV*)SvRV( $arg ));
  1285.              else{
  1286.                      warn( \"${Package}::$func_name() -- $var is not a blessed SV reference\" );
  1287.                      XSRETURN_UNDEF;
  1288.              }
  1289.  
  1290.  
  1291.      IIIInnnntttteeeerrrrffffaaaacccceeee SSSSttttrrrraaaatttteeeeggggyyyy
  1292.  
  1293.      When designing an interface between Perl and a C library a straight
  1294.      translation from C to XS is often sufficient.  The interface will often
  1295.      be very C-like and occasionally nonintuitive, especially when the C
  1296.      function modifies one of its parameters.  In cases where the programmer
  1297.      wishes to create a more Perl-like interface the following strategy may
  1298.      help to identify the more critical parts of the interface.
  1299.  
  1300.      Identify the C functions which modify their parameters.  The XSUBs for
  1301.      these functions may be able to return lists to Perl, or may be candidates
  1302.      to return undef or an empty list in case of failure.
  1303.  
  1304.      Identify which values are used by only the C and XSUB functions
  1305.      themselves.  If Perl does not need to access the contents of the value
  1306.      then it may not be necessary to provide a translation for that value from
  1307.      C to Perl.
  1308.  
  1309.      Identify the pointers in the C function parameter lists and return
  1310.      values.  Some pointers can be handled in XS with the & unary operator on
  1311.      the variable name while others will require the use of the * operator on
  1312.      the type name.  In general it is easier to work with the & operator.
  1313.  
  1314.  
  1315.  
  1316.  
  1317.                                                                        PPPPaaaaggggeeee 22220000
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1325.  
  1326.  
  1327.  
  1328.      Identify the structures used by the C functions.  In many cases it may be
  1329.      helpful to use the T_PTROBJ typemap for these structures so they can be
  1330.      manipulated by Perl as blessed objects.
  1331.  
  1332.      PPPPeeeerrrrllll OOOObbbbjjjjeeeeccccttttssss AAAAnnnndddd CCCC SSSSttttrrrruuuuccccttttuuuurrrreeeessss
  1333.  
  1334.      When dealing with C structures one should select either TTTT____PPPPTTTTRRRROOOOBBBBJJJJ or
  1335.      TTTT____PPPPTTTTRRRRRRRREEEEFFFF for the XS type.  Both types are designed to handle pointers to
  1336.      complex objects.  The T_PTRREF type will allow the Perl object to be
  1337.      unblessed while the T_PTROBJ type requires that the object be blessed.
  1338.      By using T_PTROBJ one can achieve a form of type-checking because the
  1339.      XSUB will attempt to verify that the Perl object is of the expected type.
  1340.  
  1341.      The following XS code shows the _g_e_t_n_e_t_c_o_n_f_i_g_e_n_t() function which is used
  1342.      with ONC+ TIRPC.  The _g_e_t_n_e_t_c_o_n_f_i_g_e_n_t() function will return a pointer to
  1343.      a C structure and has the C prototype shown below.  The example will
  1344.      demonstrate how the C pointer will become a Perl reference.  Perl will
  1345.      consider this reference to be a pointer to a blessed object and will
  1346.      attempt to call a destructor for the object.  A destructor will be
  1347.      provided in the XS source to free the memory used by _g_e_t_n_e_t_c_o_n_f_i_g_e_n_t().
  1348.      Destructors in XS can be created by specifying an XSUB function whose
  1349.      name ends with the word DDDDEEEESSSSTTTTRRRROOOOYYYY.  XS destructors can be used to free
  1350.      memory which may have been malloc'd by another XSUB.
  1351.  
  1352.           struct netconfig *getnetconfigent(const char *netid);
  1353.  
  1354.      A typedef will be created for struct netconfig.  The Perl object will be
  1355.      blessed in a class matching the name of the C type, with the tag Ptr
  1356.      appended, and the name should not have embedded spaces if it will be a
  1357.      Perl package name.  The destructor will be placed in a class
  1358.      corresponding to the class of the object and the PREFIX keyword will be
  1359.      used to trim the name to the word DESTROY as Perl will expect.
  1360.  
  1361.           typedef struct netconfig Netconfig;
  1362.  
  1363.           MODULE = RPC  PACKAGE = RPC
  1364.  
  1365.           Netconfig *
  1366.           getnetconfigent(netid)
  1367.                char *netid
  1368.  
  1369.           MODULE = RPC  PACKAGE = NetconfigPtr  PREFIX = rpcb_
  1370.  
  1371.           void
  1372.           rpcb_DESTROY(netconf)
  1373.                Netconfig *netconf
  1374.                CODE:
  1375.                printf("Now in NetconfigPtr::DESTROY\n");
  1376.                free( netconf );
  1377.  
  1378.      This example requires the following typemap entry.  Consult the typemap
  1379.      section for more information about adding new typemaps for an extension.
  1380.  
  1381.  
  1382.  
  1383.                                                                        PPPPaaaaggggeeee 22221111
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1391.  
  1392.  
  1393.  
  1394.           TYPEMAP
  1395.           Netconfig *  T_PTROBJ
  1396.  
  1397.      This example will be used with the following Perl statements.
  1398.  
  1399.           use RPC;
  1400.           $netconf = getnetconfigent("udp");
  1401.  
  1402.      When Perl destroys the object referenced by $netconf it will send the
  1403.      object to the supplied XSUB DESTROY function.  Perl cannot determine, and
  1404.      does not care, that this object is a C struct and not a Perl object.  In
  1405.      this sense, there is no difference between the object created by the
  1406.      _g_e_t_n_e_t_c_o_n_f_i_g_e_n_t() XSUB and an object created by a normal Perl subroutine.
  1407.  
  1408.      TTTThhhheeee TTTTyyyyppppeeeemmmmaaaapppp
  1409.  
  1410.      The typemap is a collection of code fragments which are used by the
  1411.      xxxxssssuuuubbbbpppppppp compiler to map C function parameters and values to Perl values.
  1412.      The typemap file may consist of three sections labeled TYPEMAP, INPUT,
  1413.      and OUTPUT.  The INPUT section tells the compiler how to translate Perl
  1414.      values into variables of certain C types.  The OUTPUT section tells the
  1415.      compiler how to translate the values from certain C types into values
  1416.      Perl can understand.  The TYPEMAP section tells the compiler which of the
  1417.      INPUT and OUTPUT code fragments should be used to map a given C type to a
  1418.      Perl value.  Each of the sections of the typemap must be preceded by one
  1419.      of the TYPEMAP, INPUT, or OUTPUT keywords.
  1420.  
  1421.      The default typemap in the ext directory of the Perl source contains many
  1422.      useful types which can be used by Perl extensions.  Some extensions
  1423.      define additional typemaps which they keep in their own directory.  These
  1424.      additional typemaps may reference INPUT and OUTPUT maps in the main
  1425.      typemap.  The xxxxssssuuuubbbbpppppppp compiler will allow the extension's own typemap to
  1426.      override any mappings which are in the default typemap.
  1427.  
  1428.      Most extensions which require a custom typemap will need only the TYPEMAP
  1429.      section of the typemap file.  The custom typemap used in the
  1430.      _g_e_t_n_e_t_c_o_n_f_i_g_e_n_t() example shown earlier demonstrates what may be the
  1431.      typical use of extension typemaps.  That typemap is used to equate a C
  1432.      structure with the T_PTROBJ typemap.  The typemap used by
  1433.      _g_e_t_n_e_t_c_o_n_f_i_g_e_n_t() is shown here.  Note that the C type is separated from
  1434.      the XS type with a tab and that the C unary operator * is considered to
  1435.      be a part of the C type name.
  1436.  
  1437.           TYPEMAP
  1438.           Netconfig *<tab>T_PTROBJ
  1439.  
  1440.      Here's a more complicated example: suppose that you wanted struct
  1441.      netconfig to be blessed into the class Net::Config.  One way to do this
  1442.      is to use underscores (_) to separate package names, as follows:
  1443.  
  1444.  
  1445.  
  1446.  
  1447.  
  1448.  
  1449.                                                                        PPPPaaaaggggeeee 22222222
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1457.  
  1458.  
  1459.  
  1460.              typedef struct netconfig * Net_Config;
  1461.  
  1462.      And then provide a typemap entry T_PTROBJ_SPECIAL that maps underscores
  1463.      to double-colons (::), and declare Net_Config to be of that type:
  1464.  
  1465.              TYPEMAP
  1466.              Net_Config      T_PTROBJ_SPECIAL
  1467.  
  1468.              INPUT
  1469.              T_PTROBJ_SPECIAL
  1470.                      if (sv_derived_from($arg, \"${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\")) {
  1471.                              IV tmp = SvIV((SV*)SvRV($arg));
  1472.                      $var = ($type) tmp;
  1473.                      }
  1474.                      else
  1475.                              croak(\"$var is not of type ${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\")
  1476.  
  1477.              OUTPUT
  1478.              T_PTROBJ_SPECIAL
  1479.                      sv_setref_pv($arg, \"${(my $ntt=$ntype)=~s/_/::/g;\$ntt}\",
  1480.                      (void*)$var);
  1481.  
  1482.      The INPUT and OUTPUT sections substitute underscores for double-colons on
  1483.      the fly, giving the desired effect.  This example demonstrates some of
  1484.      the power and versatility of the typemap facility.
  1485.  
  1486. EEEEXXXXAAAAMMMMPPPPLLLLEEEESSSS
  1487.      File RPC.xs: Interface to some ONC+ RPC bind library functions.
  1488.  
  1489.           #include "EXTERN.h"
  1490.           #include "perl.h"
  1491.           #include "XSUB.h"
  1492.  
  1493.           #include <rpc/rpc.h>
  1494.  
  1495.           typedef struct netconfig Netconfig;
  1496.  
  1497.           MODULE = RPC  PACKAGE = RPC
  1498.  
  1499.           SV *
  1500.           rpcb_gettime(host="localhost")
  1501.                char *host
  1502.                PREINIT:
  1503.                time_t  timep;
  1504.                CODE:
  1505.                ST(0) = sv_newmortal();
  1506.                if( rpcb_gettime( host, &timep ) )
  1507.                     sv_setnv( ST(0), (double)timep );
  1508.  
  1509.           Netconfig *
  1510.           getnetconfigent(netid="udp")
  1511.                char *netid
  1512.  
  1513.  
  1514.  
  1515.                                                                        PPPPaaaaggggeeee 22223333
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1523.  
  1524.  
  1525.  
  1526.           MODULE = RPC  PACKAGE = NetconfigPtr  PREFIX = rpcb_
  1527.  
  1528.           void
  1529.           rpcb_DESTROY(netconf)
  1530.                Netconfig *netconf
  1531.                CODE:
  1532.                printf("NetconfigPtr::DESTROY\n");
  1533.                free( netconf );
  1534.  
  1535.      File typemap: Custom typemap for RPC.xs.
  1536.  
  1537.           TYPEMAP
  1538.           Netconfig *  T_PTROBJ
  1539.  
  1540.      File RPC.pm: Perl module for the RPC extension.
  1541.  
  1542.           package RPC;
  1543.  
  1544.           require Exporter;
  1545.           require DynaLoader;
  1546.           @ISA = qw(Exporter DynaLoader);
  1547.           @EXPORT = qw(rpcb_gettime getnetconfigent);
  1548.  
  1549.           bootstrap RPC;
  1550.           1;
  1551.  
  1552.      File rpctest.pl: Perl test program for the RPC extension.
  1553.  
  1554.           use RPC;
  1555.  
  1556.           $netconf = getnetconfigent();
  1557.           $a = rpcb_gettime();
  1558.           print "time = $a\n";
  1559.           print "netconf = $netconf\n";
  1560.  
  1561.           $netconf = getnetconfigent("tcp");
  1562.           $a = rpcb_gettime("poplar");
  1563.           print "time = $a\n";
  1564.           print "netconf = $netconf\n";
  1565.  
  1566.  
  1567. XXXXSSSS VVVVEEEERRRRSSSSIIIIOOOONNNN
  1568.      This document covers features supported by xsubpp 1.935.
  1569.  
  1570. AAAAUUUUTTTTHHHHOOOORRRR
  1571.      Dean Roehrich <_r_o_e_h_r_i_c_h@_c_r_a_y._c_o_m> Jul 8, 1996
  1572.  
  1573.  
  1574.  
  1575.  
  1576.  
  1577.  
  1578.  
  1579.  
  1580.  
  1581.                                                                        PPPPaaaaggggeeee 22224444
  1582.  
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588. PPPPEEEERRRRLLLLXXXXSSSS((((1111))))                                                            PPPPEEEERRRRLLLLXXXXSSSS((((1111))))
  1589.  
  1590.  
  1591.  
  1592.  
  1593.  
  1594.  
  1595.  
  1596.  
  1597.  
  1598.  
  1599.  
  1600.  
  1601.  
  1602.  
  1603.  
  1604.  
  1605.  
  1606.  
  1607.  
  1608.  
  1609.  
  1610.  
  1611.  
  1612.  
  1613.  
  1614.  
  1615.  
  1616.  
  1617.  
  1618.  
  1619.  
  1620.  
  1621.  
  1622.  
  1623.  
  1624.  
  1625.  
  1626.  
  1627.  
  1628.  
  1629.  
  1630.  
  1631.  
  1632.  
  1633.  
  1634.  
  1635.  
  1636.  
  1637.  
  1638.  
  1639.  
  1640.  
  1641.  
  1642.  
  1643.  
  1644.                                                                        PPPPaaaaggggeeee 22225555
  1645.  
  1646.  
  1647.  
  1648.  
  1649.  
  1650.  
  1651.